import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;

import static junit.framework.TestCase.assertNotNull;
import static org.junit.Assert.assertEquals;

/**
 * author:wzq
 * project:01_hello
 * date:16-5-11
 */
public class _01_SimpleBookOrderTest_02 {

    @Test
    public void startBookOrder() {
        // create ProcessEngine, use default activiti.cfg.xml as configuration
        // standalone mode, not need activiti.cfg.xml
        ProcessEngine processEngine = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration()
                .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE)
                .setJdbcUrl("jdbc:h2:mem:my-own-db;DB_CLOSE_DELAY=1000")
                .setAsyncExecutorEnabled(true)
                .setAsyncExecutorActivate(false)
                .buildProcessEngine();

        RuntimeService runtimeService = processEngine.getRuntimeService();

        RepositoryService repositoryService = processEngine.getRepositoryService();

        // deploy a process definition
        repositoryService.createDeployment()
                .addClasspathResource("_01_bookorder.simple.bpmn20.xml")
                .deploy();

        // check the deployment, the key simplebookorder from the bpmn20.xml file
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().singleResult();
        assertEquals("simplebookorder", processDefinition.getKey());

        // start a process instance
        ProcessInstance processInstance = runtimeService
                .startProcessInstanceByKey("simplebookorder");

        assertNotNull(processInstance.getId());
        System.out.println("id " + processInstance.getId() + " " + processInstance.getProcessDefinitionId());
    }
}
